import { createRouter, createWebHashHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import Localcache from '@/utils/cache'
import { firtMenu } from '@/utils/map-menus'
const routes: RouteRecordRaw[] = [
  {
    path: '/',
    redirect: '/main'
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('views/login/login.vue')
  },
  {
    path: '/main',
    name: 'main',
    component: () => import('views/main/main.vue')
  },
  {
    path: '/:pathMacth(.*)*',
    name: 'notFound',
    component: () => import('views/not-found/notFound.vue')
  }
]

const router = createRouter({
  routes: routes,
  history: createWebHashHistory()
})
router.beforeEach((to) => {
  if (to.path !== '/login') {
    const token = Localcache.getcache('token')
    if (!token) {
      router.replace('/login')
    }
  }
  if (to.path === '/main') {
    return firtMenu.path
  }
})
export default router
